@echo off
setlocal

:: 读取参数
set "ARG1=%~1"
set "ARG2=%~2"

:: 默认值
set "GENERATOR=MinGW Makefiles"
set "BUILD_TYPE=release"
set "BUILD_DIR=%cd%\build\MinGW"

:: 参数逻辑处理
if "%ARG1%"=="" goto NoGenerator
if /i "%ARG1%"=="vs" goto UseVS
if /i "%ARG1%"=="mingw" goto UseMinGW
goto UseCustomGenerator

:NoGenerator
:: 没有传参，使用默认 MinGW + Release
goto End

:UseVS
:: 使用 Visual Studio 生成器
set "GENERATOR=Visual Studio 17 2022"
set "BUILD_DIR=%cd%\build\VS"
if "%ARG2%"=="" (
    set "BUILD_TYPE=release"
) else (
    set "BUILD_TYPE=%ARG2%"
)
goto End

:UseMinGW
:: 使用 MinGW Makefiles 生成器
set "GENERATOR=MinGW Makefiles"
set "BUILD_DIR=%cd%\build\MinGW"
if "%ARG2%"=="" (
    set "BUILD_TYPE=release"
) else (
    set "BUILD_TYPE=%ARG2%"
)
goto End

:UseCustomGenerator
:: 使用自定义生成器
set "GENERATOR=%ARG1%"
set "BUILD_DIR=%cd%\build\MinGW"
if "%ARG2%"=="" (
    set "BUILD_TYPE=release"
) else (
    set "BUILD_TYPE=%ARG2%"
)

:End
echo Build Tool: %GENERATOR%
echo Build Type: %BUILD_TYPE%
echo Build Dir: %BUILD_DIR%

:: 判断文件夹是否存在，如果不存在则创建
if not exist "%BUILD_DIR%" (
    echo Create Build Dir: %BUILD_DIR%
    mkdir "%BUILD_DIR%"
) else (
    echo Build Dir Existed: %BUILD_DIR%
)

cd /d "%BUILD_DIR%"

:: 调用 CMake 生成项目
cmake ../.. -G "%GENERATOR%" -DCMAKE_BUILD_TYPE=%BUILD_TYPE%

:: 构建
cmake --build . --config %BUILD_TYPE%

endlocal
